// pages/carInformation/index.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addLicensePlates: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync('userId')
    wx.request({
      //车辆查询
      url: app.globalData.basicUrl + 'user/userPlate/userId/' + userId,
      // data:1,
      success: function (res) {
        console.log(res);
        res.data.data.map(item => {
          if (item.carType == '1') {
            item.carType = '新能源汽车'
          } else {
            item.carType = '燃油汽车'

          }
        })
        that.setData({
          addLicensePlates: res.data.data
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.addcar = this.selectComponent("#addcar");
  },
  //添加车辆
  addcars: function (e) {
    var that = this;
    console.log("我输入的值", this.addcar.data)
    var card = this.addcar.data.inputPlates
    var carType = this.addcar.data.flag?'0':'1'  //切换状态
    var pate = card.index0 + card.index1 + card.index2 + card.index3 + card.index4 + card.index5 + card.index6+card.index7
    console.log("我输入的车牌号", pate.length)
    let userId = wx.getStorageSync('userId')
    var par = {
      "userId": userId,
      "plateNo": pate,
      "carType": carType,
      "delFlag": "0"
    }
    var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
    var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    let result = false;
    if (pate.length == 7) {
      result = creg.test(pate);
    } else if (pate.length == 8) {
      result = xreg.test(pate);
    } else {
      result = false;
    }
    if (result) {
      wx.request({
        url: app.globalData.basicUrl + 'user/userPlate',
        method: "POST",
        data: par,
        success: function (res) {
          console.log("当前的res", res);
          that.onLoad();//添加车辆
          //var msg = res.data.msg
          // wx.showToast({
          //   title: msg,
          // })
          wx.showToast({
            title: '添加成功',
            duration: 2000,
            success: function () {
              setTimeout(() => {
                if(that.options.url=='pay'){
                  wx.switchTab({
                    url: '/pages/play/index',
                  })
                }else{
                  wx.switchTab({
                    url: '/pages/ucenter/my/index',
                  })
                }
              }, 2000)
            }
          })
        },
        fail(res){
          wx.showToast({
            icon:'none',
            title: '添加失败',
            duration: 2000,
          })
        }
      })
    }else{
      wx.showToast({
        icon:'none',
        title: '车牌号格式不正确',
        duration: 2000,
      })
    }
    
  },
  
  /**
   * 弹窗
   */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
    wx.showModal({
      title: '解绑成功',
      confirmText: "确定",
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
    console.log("解绑成功");
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
    wx.showModal({
      icon: "success",
      title: '取消成功',
      confirmText: "确定",
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
    console.log("取消成功");
  },





  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})